﻿<%@ WebHandler Language="C#" Class="HanderAjax" %>

using System;
using System.Web;
using System.Data;
using System.Web.SessionState;

public class HanderAjax : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        Access access = new Access();
        context.Response.ContentType = "text/plain";
        if (GetQeuryString("para", context) == "asyncLogin")
        {

            try
            {
                string uid = GetQeuryString("uid", context);
                string pwd = GetQeuryString("pwd", context);
              
                DataSet ds = access.Veiry(uid, pwd);
                
                if (ds.Tables[0].Rows.Count > 0)
                {
                    Model.UserInfo user = new Model.UserInfo();
                    DataRow dr = ds.Tables[0].Rows[0];
                    user.ID = Convert.ToInt32(dr["id"]);
                    user.UserName = dr["userName"].ToString();
                    user.UID = dr["uid"].ToString();
                    user.PWD = dr["pwd"].ToString();
                    user.Sex = dr["sex"].ToString();
                    user.CreateTime = Convert.ToDateTime(dr["createTime"]);
                    user.UpdateTime = Convert.ToDateTime(dr["UpdateTime"]);
                    user.Skin = dr["skin"].ToString();
                    user.MID = int.Parse(dr["mid"].ToString());
                    user.Name = dr["name"].ToString();
                    context.Session["UserInfo"] = user;
                    context.Response.Write("1");
                }
                else context.Response.Write("0");
                //context.Response.End();
            }
            catch (Exception ex)
            {
                context.Response.Clear();
                context.Response.Write(ex.Message);
                //context.Response.End();
            }

        }
        else if (GetQeuryString("para", context) == "treeMenu")//树菜单
        {
            
            string parentid = "0";
            if (context.Request.Params["id"] != null)
            {
                parentid = context.Request.Params["id"];
            }
            if (context.Session["UserInfo"] != null)
            {
                Model.UserInfo u = (Model.UserInfo)context.Session["UserInfo"];
                DataSet ds = access.GetMenuData(parentid,u.ID.ToString());
                string re = ToJson.GetTreeJsonByTableAsync(ds.Tables[0], "id", "MenuName", "parentid", parentid, "menuurl", new string[] { });
                context.Response.Write(re);
            }
        }
        else if (GetQeuryString("para", context) == "signOut")
        {
            context.Session["UserInfo"] = null;
            context.Response.Write("1");
        }
        else if (GetQeuryString("para", context) == "getMenuTree")
        {
            string parentid = "0";
            if (context.Request.Params["id"] != null)
            {
                parentid = context.Request.Params["id"];
            }
            if (context.Session["UserInfo"] != null)
            {
                Model.UserInfo u = (Model.UserInfo)context.Session["UserInfo"];

                DataSet ds = access.GetMenuData(parentid,null);
                string[] ss = new string[] { "MenuName", "MenuIcon", "Suqence", "CreateTime", "UpdateTime", "ParentID", "MenuUrl" };
                string re = ToJson.GetTreeJsonByTableAsync(ds.Tables[0], "id", "MenuName", "parentid", parentid, "menuurl", ss);
                context.Response.Write(re);
            }
        }
        //else if (GetQeuryString("para", context) == "getMenuTree2")
        //{
        //    string parentid = "0";
        //    //if (context.Request.Params["id"] != null)
        //    //{
        //    //    parentid = context.Request.Params["id"];
        //    //}
        //    //if (context.Session["UserInfo"] != null)
        //    //{
        //        Model.UserInfo u = (Model.UserInfo)context.Session["UserInfo"];

        //        DataSet ds = access.GetMenuData2();
        //        string[] ss = new string[] { "MenuName", "MenuIcon", "Suqence", "CreateTime", "UpdateTime", "ParentID", "MenuUrl" };
        //        string re = ToJson.GetTreeJsonByTableAsync(ds.Tables[0], "id", "MenuName", "parentid", parentid, "menuurl", ss);
        //        context.Response.Write(re);
        //    //}
        //}
        else if (GetQeuryString("para", context) == "getWeiMenuTree") //微信菜单
        {
            string parentid = "0";
            if (context.Request.Params["id"] != null)
            {
                parentid = context.Request.Params["id"];
            }
            if (context.Session["UserInfo"] != null)
            {
                Model.UserInfo u = (Model.UserInfo)context.Session["UserInfo"];

                DataSet ds = access.GetWeiMenuData(parentid, u.MID);
                string[] ss = new string[] { "Name", "Keys", "Suqence", "ParentID", "MID" };
                string re = ToJson.GetTreeJsonByTableAsync(ds.Tables[0], "id", "Name", "parentid", parentid, "MID", ss);
                context.Response.Write(re);
            }
        }
        else if (GetQeuryString("para", context) == "UpdateSkin")
        {
            string Skin = GetQeuryString("Skin", context);
            Model.UserInfo user = (Model.UserInfo)context.Session["UserInfo"];
            user.Skin = Skin;
            context.Session["UserInfo"] = user;//修改session
            access.UpdateUserInfo(user);
        }
        else if (GetQeuryString("para", context) == "deleteMenu")
        {
            try
            {
                string id = GetQeuryString("id", context);
                access.DeleteMenu(id);
                context.Response.Write("ok");
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }
        else if (GetQeuryString("para", context) == "deleteWeiMenu")
        {
            try
            {
                string id = GetQeuryString("id", context);
                access.DeleteWeiMenu(id);
                context.Response.Write("ok");
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message);
            }
        }
        else if (GetQeuryString("para", context) == "GetUserList")
        {
            string s = "{\"total\":\"239\",\"rows\":[  {\"code\":\"001\",\"name\":\"Name 1\",\"addr\":\"Address 11\",\"addr\":\"col4 data\"}   ]}  ";

            context.Response.Write(s);
        }
        else if (GetQeuryString("para", context) == "GetUserList")
        {
            DataSet ds = access.GetAllUser(" order by id asc", 1, 10, "");
            string re = ToJson.DataTableToJson(ds.Tables[0].Rows[0][0].ToString(), "rows", ds.Tables[1]);
        }

    }
    public string GetQeuryString(string para, HttpContext context)
    {
        if (context.Request.QueryString[para] != null) return context.Request.QueryString[para].ToString();
        else return "";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }



}